#ifndef _CM_DBLITEMANAGER_H
#define _CM_DBLITEMANAGER_H

/******************************************************************************
** @file	cm_registryitem.h
** @author	Nestor Felipe
** @author	Joel Chuca Condo
**
** @date	Sep 01, 2010
**
** @desc	This class managments to persist wmi objects into database. 
**          Its main functionality is crate a database and convert wmi objects 
**          to sql code
**
******************************************************************************/

#include <wmi_reader.h>
#include "adcm_computer.h"
#include "cm_scripts.h"
#include "cm_collectionitem.h"
#include "cm_shareitem.h"
#include "cm_registryitem.h"
#include "cm_filedirectoryitem.h"
#include "cm_processitem.h"
#include "cm_resultCollection.h"
#include "sqlite3x.hpp"
#include "adcm_domain.h"

class CMEXPORT CM_DBLiteManager {
public:

    /**
    * Default constructor
    */
    CM_DBLiteManager();

    /**
    * Destructor
    */
	~CM_DBLiteManager();

    /**
    * Initializes the database by creating the file and the respective tables.
    *
    * @param path. the path to crate the database
    * @retun a integer value indicating the success of the process
    */
	int 
    initialize(const wstring& path);

    /**
    * Initializes the database by creating the file and the respective tables.
    * this method create the database with a default path. 
    *
    * @retun a integer value indicating the success of the process
    */
	int 
    initialize();
	
    /**
    * Drops de database
    *
    * @retun a integer value indicating the success of the process
    */
    int 
    drop();
	
    /**
    * Save a domain object into database
    *
    * @param domain. the domain object
    * @retun a integer value indicating the success of the process
    */
	int 
	saveDomain(ADCM_Domain& domain);

    /**
    * get a domain object from database.
    *
    * @param domainID. the domain id of the database.
    * @param domain.[out] the domain result.
    * @retun a integer value indicating the success of the process
    */
	int 
	getDomain(int idDomain, ADCM_Domain& domain);

	/**
    * Save a computer object into database, 
	* only if the computer not exist
    *
    * @param domainID. the domain id of the database
    * @param computer. the computer object
    * @retun a integer value indicating the success of the process
    */
	int 
    saveComputer(int domainID, ADCM_Computer& computer);

	/**
    * Get computer from a given domain.
    *
    * @param domainID. the id of the domain to retrieve computers
    * @param vector<ADCM_Computer> [out] the result, vector of computers found
    * @retun a integer value indicating the success of the process
    */
	int 
    getComputers(int domainID, vector<ADCM_Computer>& );

	/**
    * Get computer from a given full qualified domain name.
    *
    * @param wstring. the full qualified domain name to retrieve computers.
    * @param vector<ADCM_Computer> [out] the result, vector of computers found.
    * @retun a integer value indicating the success of the process.
    */
	int 
	getComputers(const wstring fqdn, vector<ADCM_Computer>& );

    /**
    * Saves a collection of object results of a computer in a instant of time 
    *
    * @param collectionId. The computer id of the database
    * @param collection. collection of objects
	*
    * @retun a integer value indicating the success of the process
    */
    int 
    saveResultCollection(int computerId, CM_ResultCollection& collection);

    /**
    * Gets the last collection of object of a computer that was inserted
    * into database
    *
    * @param computerId. The computer id of database.
    * @param collection. Reference to a collection of objects
	*
    * @retun an integer value indicating the success of the process
    */
	int 
    getLastCollection(int computerId, CM_ResultCollection& collection);

    /**
    * Gets the collection of object of a computer that was inserted
    * before of the last
    *
    * @param computerId. The computer id of database.
    * @param collection. Reference to a collection of objects
	*
    * @retun an integer value indicating the success of the process
    */
	int 
    getBeforeLastCollection(int computerId, CM_ResultCollection& collection);
	
private:

    /**
	* if a computer exist or not inside database. the logic to 
	* determinate if exist, its search a computer with 
	* equal cm, (common name) attribute inside the domain given (represent by idDomain)
	* 
	* @param idDomain	identifier of domain.
	* @param computer	the computer to verify.
	*
	* @retun	'true' if exist, otherwise 'false'.
	*/
	bool 
	exist(int idDomain, ADCM_Computer& computer);	

	bool exist(ADCM_Domain& domain);

	void deleteComputers(ADCM_Domain& domain);
};

#endif  //_CM_DBLITEMANAGER_H

